﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using WorkflowAcessLayer.ExpenseWorkflow;

public class ExpenseWorkflowAcessLayer
{

    public static bool SubmitExpense(Guid report)
    {

        ExpenseWorkflowContractClient client = new ExpenseWorkflowContractClient();           
        
        try
        {
            client.SubmitExpense(report);
            return true;
        }
        catch (Exception e)
        {
            using (DataAccessDataContext dc = new DataAccessDataContext())
            {
                var q = from rep in dc.ExpenseReports
                        where rep.Id.Equals(report)
                        select rep;

                ExpenseReport er = q.Single<ExpenseReport>();


                var query = from elines in dc.ExpenseLines
                            where elines.ExpenseReportId.Equals(report)
                            select elines;

                //PARA CADA LINHA DE DESPESA EM REPORT ID
                WorkflowQueue wq;
                foreach (ExpenseLine el in query)
                {
                    wq = new WorkflowQueue();
                    wq.ExpenseId = el.Id;
                    wq.UserSubmitedId = er.UserEmail;
                    wq.Date = DateTime.Now.ToLocalTime();
                    wq.Type = 0;
                    dc.WorkflowQueues.InsertOnSubmit(wq);
                }

                dc.SubmitChanges();
                return false;
            }
        }
        finally { client.Close(); }
    }

    public static bool Authorized(Guid expenseid, String userid)
    {
        ExpenseWorkflowContractClient client = new ExpenseWorkflowContractClient();
        try
        {
            client.Authorized(expenseid, userid);
            return true;
        }
        catch (Exception e)
        {
            using (DataAccessDataContext dc = new DataAccessDataContext())
            {

                WorkflowQueue wq = new WorkflowQueue();
                wq.ExpenseId = expenseid;
                wq.UserSubmitedId = userid;
                wq.Date = DateTime.Now.ToLocalTime();
                wq.Type = 1;
                dc.WorkflowQueues.InsertOnSubmit(wq);
                dc.SubmitChanges();
                return false;
            }
        }
        finally { client.Close(); }
    }

    public static bool Rejected(Guid expenseid, String userid, String justification)
    {
        ExpenseWorkflowContractClient client = new ExpenseWorkflowContractClient();
        try
        {
            client.Rejected(expenseid, userid,justification);
            return true;
        }
        catch (Exception e)
        {
            using (DataAccessDataContext dc = new DataAccessDataContext())
            {

                WorkflowQueue wq = new WorkflowQueue();
                wq.ExpenseId = expenseid;
                wq.UserSubmitedId = userid;
                wq.Date = DateTime.Now.ToLocalTime();
                wq.Type = 2;
                wq.Justification = justification;
                dc.WorkflowQueues.InsertOnSubmit(wq);
                dc.SubmitChanges();
                return false;
            }
        }
        finally { client.Close(); }
    }



}

